﻿using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace ZTZP_BEMSS_WPF
{
    /// <summary>
    /// RedHeartBeatingDialog07.xaml 的交互逻辑
    /// </summary>
    public partial class RedHeartBeatingDialog07 : Window
    {
        // 静态变量，保存唯一实例
        private static RedHeartBeatingDialog07 _instance;

        // 私有化构造函数
        private RedHeartBeatingDialog07()
        {
            InitializeComponent();  // Initialize the components from the XAML
        }

        // 获取唯一实例的方法
        public static RedHeartBeatingDialog07 Instance
        {
            get
            {
                if (_instance == null)
                {
                    // 如果实例不存在，则创建一个新的实例
                    _instance = new RedHeartBeatingDialog07();
                }
                return _instance;
            }
        }

        // 如果对话框已存在，确保将其显示出来而不创建新的实例
        public static void ShowDialogOnce()
        {
            if (_instance == null)
            {
                _instance = new RedHeartBeatingDialog07();
            }
            _instance.Show();  // Use ShowDialog() to block the thread if needed
        }

        // 在窗口加载时启动动画
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            // Apply scale transform to the heart image to allow scaling animation
            HeartImage.RenderTransform = new ScaleTransform(1, 1);

            // Start the heartbeat animation
            Storyboard heartBeatStoryboard = new Storyboard();

            // Create ScaleX animation
            DoubleAnimation scaleXAnimation = new DoubleAnimation
            {
                From = 1,
                To = 1.2,
                Duration = TimeSpan.FromSeconds(0.3),
                AutoReverse = true,
                RepeatBehavior = RepeatBehavior.Forever
            };
            //Storyboard.SetTarget(scaleXAnimation, HeartScaleTransform);
            Storyboard.SetTargetProperty(scaleXAnimation, new PropertyPath(ScaleTransform.ScaleXProperty));

            // Create ScaleY animation
            DoubleAnimation scaleYAnimation = new DoubleAnimation
            {
                From = 1,
                To = 1.2,
                Duration = TimeSpan.FromSeconds(0.3),
                AutoReverse = true,
                RepeatBehavior = RepeatBehavior.Forever
            };
            //Storyboard.SetTarget(scaleYAnimation, HeartScaleTransform);
            Storyboard.SetTargetProperty(scaleYAnimation, new PropertyPath(ScaleTransform.ScaleYProperty));

            // Add animations to storyboard
            heartBeatStoryboard.Children.Add(scaleXAnimation);
            heartBeatStoryboard.Children.Add(scaleYAnimation);

            // Start the animation
            heartBeatStoryboard.Begin();
        }

        // Close button click event handler
        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            // Close the dialog
            this.Close();
        }
    }
}
